package game.view;

import game.input.MovementController;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JFrame;

import console.view.CommandHandler;

import accessory.CLog;
import accessory.Command;
import accessory.cres.CResNode;

import map.PlayerObject;
import map.RasterCell;

public class MainFrame extends JFrame {
		
	//constants
	private static final String GRAFICS_LOADING_FRAME_WIDTH_STRING = "loading_frame_width";
	private static final String GRAFICS_LOADING_FRAME_HEIGHT_STRING = "loading_frame_height";
	private static final String GRAFICS_CONTENT_FRAME_WIDTH_STRING = "content_frame_width";
	private static final String GRAFICS_CONTENT_FRAME_HEIGHT_STRING = "content_frame_height";
	private static final String GRAFICS_CONTENT_FRAME_FULLSCREEN = "content_frame_fullscreen";
	
	private CResNode graficsNode;
	private MovementController movementController;
	private HashMap<String, GameCanvas> maps;
	
	private RosterView rosterView;
	private PlayerObject playerObject;
	
	public MainFrame(CResNode graficsNode){
		playerObject = new PlayerObject();
		this.graficsNode = graficsNode;
		this.maps = new HashMap<String, GameCanvas>();
		this.movementController = new MovementController(playerObject);
		this.rosterView = new RosterView();
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);	
		addKeyListener(movementController);
		
		//################ Keyboad Commands
		movementController.addAction("SHOW_ROSTER", new KeyboardCommand() {
			@Override
			public void execute() {
				rosterView.setActive(!rosterView.isActive());
				//movementController.setDisabled(rosterView.isActive());
				repaint();
			}
		});
		
		movementController.addAction("SURF", new KeyboardCommand() {
			@Override
			public void execute() {
				//CLog.info("surfaction -> "+playerObject.activateSurf());
			}
		});
		
		//############ Console Commands
		CommandHandler.addCommand("quit", new Command() {
			@Override
			public String execute(String[] args) {
				System.exit(0);
				return "exit";
			}
		});
	}

	
	public void loadGameContents(String mapName, RasterCell[][] map){
		setTitle("Game - "+mapName);
		
		if(!Boolean.parseBoolean(graficsNode.getNodeValue(GRAFICS_CONTENT_FRAME_FULLSCREEN))){
			resizeFrame(Integer.parseInt(graficsNode.getNodeValue(GRAFICS_CONTENT_FRAME_WIDTH_STRING)),  Integer.parseInt(graficsNode.getNodeValue(GRAFICS_CONTENT_FRAME_HEIGHT_STRING)));
						
		} else {
			//TODO:
			resizeFrame(1920, 1080);
		}
		
		if(maps.get(mapName) == null){
			GameCanvas canvas = new GameCanvas(map, playerObject, rosterView);
			canvas.setSize(getWidth(), getHeight());
			canvas.setFocusable(true);
			setContentPane(canvas);
			movementController.setCanvas(canvas);
			maps.put(mapName, canvas);
		
			playerObject.setActiveMap(map);
			playerObject.setPosition(canvas.getPov());
			
			canvas.addKeyListener(new MovementController(playerObject));
		}
	}


	public void loadDialog() {
		setTitle("Loading...");
		resizeFrame(Integer.parseInt(graficsNode.getNodeValue(GRAFICS_LOADING_FRAME_WIDTH_STRING)),  Integer.parseInt(graficsNode.getNodeValue(GRAFICS_LOADING_FRAME_HEIGHT_STRING)));

	}
	
	private void resizeFrame(int width, int height){
		setBounds(new Rectangle(width, height));
	}
}
